package com.zp.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zp.annotations.SaSpaceCheckPermission;
import com.zp.constants.SpaceUserPermissionConstant;
import com.zp.domain.common.DeleteRequest;
import com.zp.domain.common.Result;
import com.zp.domain.entity.SpaceUser;
import com.zp.domain.entity.User;
import com.zp.domain.request.spaceuser.SpaceUserAddRequest;
import com.zp.domain.request.spaceuser.SpaceUserEditRequest;
import com.zp.domain.request.spaceuser.SpaceUserQueryRequest;
import com.zp.domain.vo.SpaceUserVO;
import com.zp.enums.ErrorCode;
import com.zp.exception.BusinessException;
import com.zp.service.SpaceUserService;
import com.zp.service.UserService;
import com.zp.utils.ThrowUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/spaceUser")
@Api(tags = "空间成员模块")
@RequiredArgsConstructor
public class SpaceUserController {
    private final SpaceUserService spaceUserService;
    private final UserService userService;

    /**
     * 添加空间成员
     *
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/add")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Result<?> addSpaceUser(@RequestBody SpaceUserAddRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        spaceUserService.addSpaceUser(request);
        return Result.success();
    }

    /**
     * 删除空间成员
     *
     * @param request id
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Result<Boolean> deleteSpaceUser(@RequestBody DeleteRequest request) {
        if (request == null || request.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SpaceUser oldSpaceUser = spaceUserService.getById(request.getId());
        ThrowUtils.throwIf(ObjUtil.isNull(oldSpaceUser), ErrorCode.NOT_FOUND_ERROR, "空间成员不存在");
        // 执行删除
        boolean result = spaceUserService.removeById(request.getId());
        return Result.success(result);
    }

    /**
     * 查询指定成员在指定空间的信息
     *
     * @param request 查询参数
     * @return 查询结果
     */
    @PostMapping("/get")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Result<SpaceUser> getSpaceUser(@RequestBody SpaceUserQueryRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        Long userId = request.getUserId();
        Long spaceId = request.getSpaceId();
        if (ObjUtil.hasNull(userId, spaceId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 执行查询
        QueryWrapper<SpaceUser> queryWrapper = spaceUserService.getQueryWrapper(request);
        SpaceUser spaceUser = spaceUserService.getOne(queryWrapper);
        ThrowUtils.throwIf(ObjUtil.isNull(spaceUser), ErrorCode.NOT_FOUND_ERROR, "空间成员不存在");
        return Result.success(spaceUser);
    }

    /**
     * 查询公共空间中的所有成员
     *
     * @param request 查询参数
     * @return 成员信息
     */
    @PostMapping("/list")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Result<List<SpaceUserVO>> listSpaceUser(@RequestBody SpaceUserQueryRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        QueryWrapper<SpaceUser> queryWrapper = spaceUserService.getQueryWrapper(request);
        List<SpaceUser> spaceUserList = spaceUserService.list(queryWrapper);
        List<SpaceUserVO> spaceUserVOList = spaceUserService.getSpaceUserVOList(spaceUserList);
        return Result.success(spaceUserVOList);
    }

    /**
     * 编辑空间成员信息
     *
     * @param request 编辑参数
     * @return 是否成功
     */
    @PostMapping("edit")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Result<Boolean> editSpaceUser(@RequestBody SpaceUserEditRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        // 属性拷贝
        SpaceUser spaceUser = new SpaceUser();
        BeanUtils.copyProperties(request, spaceUser);
        // 数据校验
        spaceUserService.validSpaceUser(spaceUser, false);
        // 判断是否存在
        Long spaceUserId = request.getId();
        SpaceUser oldSpaceUser = spaceUserService.getById(spaceUserId);
        ThrowUtils.throwIf(ObjUtil.isNull(oldSpaceUser), ErrorCode.NOT_FOUND_ERROR, "空间成员不存在");
        // 执行编辑
        boolean result = spaceUserService.updateById(spaceUser);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新空间成员失败");
        return Result.success(true);
    }

    /**
     * 查询我加入的团队空间列表
     *
     * @param request 获取当前登录用户
     * @return 团队空间列表
     */
    @PostMapping("/list/my")
    public Result<List<SpaceUserVO>> listMyTeamSpace(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        SpaceUserQueryRequest spaceUserQueryRequest = new SpaceUserQueryRequest();
        spaceUserQueryRequest.setUserId(loginUser.getId());
        QueryWrapper<SpaceUser> queryWrapper = spaceUserService.getQueryWrapper(spaceUserQueryRequest);
        List<SpaceUser> spaceUserList = spaceUserService.list(queryWrapper);
        List<SpaceUserVO> spaceUserVOList = spaceUserService.getSpaceUserVOList(spaceUserList);
        return Result.success(spaceUserVOList);
    }
}
